package com.echat.business.blog.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.plugins.Page;
import com.echat.business.blog.model.Blog;
import com.echat.business.blog.model.BlogTag;
import com.echat.business.blog.service.IBlogService;
import com.echat.business.blog.service.IBlogTagRelationService;
import com.echat.business.blog.warpper.BlogWarpper;
import com.echat.business.home.model.SysUser;
import com.echat.config.common.RequestHolder;
import com.echat.core.constant.ConstantUtils;
import com.echat.core.constant.response.ResponseData;
import com.echat.core.utils.StringUtils;
import com.echat.core.utils.SubUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author chentl
 * @version V1.0
 * @Project BlogController
 * @Title BlogController.java
 * @Description blog 控制器
 * @Package com.echat.business.blog.controller
 * @date 2019/9/5 18:03
 * @Copyright: 上海顺益信息科技有限公司 All rights reserved.
 */
@RestController
@Slf4j
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private IBlogService blogService;

    @Autowired
    private IBlogTagRelationService tagRelationService;

    @RequestMapping(value = "/blogList")
    public ResponseData getBlogList(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize, @RequestParam("order") String order, @RequestParam("blogType") Integer blogType, @RequestParam("blogTag") Integer blogTag) {
        Page<Map<String, Object>> page = new Page<>();
        Blog blog = new Blog();
        blog.setBlogType(blogType);
        blog.setBlogTag(blogTag);
        page.setSize(pageSize);
        page.setCurrent(current);
        List<Map<String, Object>> list = new BlogWarpper(blogService.getBlogList(page, blog, order)).wrap();

        page.setRecords(list);
        return new ResponseData(true, 200, "成功", page);
    }

    @RequestMapping(value = "/getBlogsByType")
    public ResponseData getBlogsByTypeId(@RequestParam("blogType") Integer blogType) {
        if (blogType == null) {
            return ResponseData.error("参数为空");
        }
        List<Map<String, Object>> list = new BlogWarpper(blogService.getBlogsByBlogTypeId(blogType)).wrap();
        return ResponseData.success(200, "分类博客列表成功", list);
    }

    @RequestMapping(value = "/add")
    public ResponseData add(Blog blog) {
        if (blog == null) {
            return ResponseData.error("博客对象为空");
        }
        SysUser loginUser = RequestHolder.getCurrentUser();
        Integer loginId = loginUser.getId();
        blog.setCreateby(loginId);
        blog.setUpdateby(loginId);
        blog.setAuthor(loginId);
        blog.setBlogContent("");
        boolean flag = blogService.insert(blog);
        if (flag) {
            return ResponseData.success("新建文章成功");
        } else {
            return ResponseData.error("新建文章失败");
        }
    }

    @RequestMapping(value = "/delete")
    public ResponseData delete(Blog blog) {
        if (blog == null) {
            return ResponseData.error("博客对象为空");
        }
        //删除文章
        blogService.deleteById(blog.getId());
        //同时删除关联关系
        tagRelationService.deleteByBlogId(blog.getId());
        return ResponseData.success(200, "文章删除成功~", null);
    }

    @RequestMapping(value = "/update")
    public ResponseData update(Blog blog) {
        if (blog == null) {
            return ResponseData.error("博客对象为空");
        }
        Integer blogId = blog.getId();
        String content = blog.getBlogContent();
        String matchUrl = SubUtil.getSubUtilSimple(content, ".*?\\]\\((.*?)\\).*?");
        String imgUrl = StringUtils.isEmpty(matchUrl) ? "https://picsum.photos/500/300" : matchUrl;
        try {
            List<BlogTag> tags = JSONObject.parseObject(blog.getBlogTagList(), new TypeReference<List<BlogTag>>() {
            });
            if (CollectionUtil.isNotEmpty(tags)) {
                //重建关联
                tagRelationService.rebuildBlogTagRelation(tags, blogId);
            }
            //1 已发布 0 未发布 这里是已发布
            blog.setIsPublished(ConstantUtils.Public.ONE);
            blog.setCoverUrl(imgUrl);
            //更新博客
            blogService.updateById(blog);
            return ResponseData.success(200, "发布成功~", tags);
        } catch (Exception e) {
            log.error("博客标签json解析时出错 ", e);
            return ResponseData.error("博客标签json解析时出错");
        }
    }


}
